package com.demo.camunda.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.demo.camunda.dao.SqmWarningMappingDao;
import com.demo.camunda.entity.Msgclient;
import com.demo.camunda.entity.MsgclientT;
import com.demo.camunda.listener.ImportListener;
import com.demo.camunda.listener.ListenerExcelT;
import com.demo.camunda.service.ExcelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;


@Service
public class ExcelServiceImpl implements ExcelService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelServiceImpl.class);

@Autowired
private uploadExcelImpl excel;


@Autowired
private SqmWarningMappingDao sqmWarningMappingDao;

@Autowired
private uploadTExcelImpl uploadTExcel;

    @Override
    public void uploadExcel(MultipartFile file) {
//        // 转换request，解析出request中的文件
//        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//       
//        // 获取文件map集合
//        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
//        if (CollectionUtil.isNullOrEmpty(fileMap)) {
//            throw new ApplicationIllegalArgumentException(ExceptionMsg.SAB_UPLOAD_INIT_DATA_ERROR);
//        }
//
//        MultipartFile file = fileMap.get("file");

        if (file.isEmpty()) {
           // throw new ApplicationIllegalArgumentException(ExceptionMsg.SAB_UPLOAD_INIT_DATA_ERROR);
        }
        String fileName = file.getOriginalFilename();
        /**
         * 校验文件后缀
         */
        if (fileName.toLowerCase().endsWith(".csv") || fileName.toLowerCase().endsWith(".xlsx")) {
            Date start = new Date();


                try {

//
     //             EasyExcel.read(file.getInputStream(), Msgclient.class, new ImportListener(excel)).sheet(0).doRead();
//                    EasyExcel.read(file.getInputStream(), MsgclientT.class, new ListenerExcelT(uploadTExcel)).sheet(1).doRead();

                    ExcelReader excelReader = EasyExcel.read(file.getInputStream()).build();

                   ImportListener importListener1 = new ImportListener(excel,sqmWarningMappingDao);
                  //  ListenerExcelT<MsgclientT> listenerExcelT = new ListenerExcelT<>(uploadTExcel);
                    ReadSheet readSheet0 = EasyExcel.readSheet(0).registerReadListener(importListener1).build();

                 //   ReadSheet readSheet1 = EasyExcel.readSheet(1).head(MsgclientT.class).registerReadListener(listenerExcelT).build();
                    excelReader.read( readSheet0);


               //      这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                    excelReader.finish();

                LOGGER.info("上传成功");
                LOGGER.info("end,time is {}", ((new Date().getTime() - start.getTime()) / 1000));
            } catch (Exception e) {
                e.printStackTrace();
            }
         
        } else {
            // "只允许上传csv和xlsx文件"
        //    throw new ApplicationIllegalArgumentException(ExceptionMsg.SAB_UPLOAD_INIT_DATA_ERROR);
        }


    }
}
